//
//  UserStatusCell.m
//  Guazi
//
//  Created by koolearn on 11-8-5.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "UserStatusCell.h"
#import "TweetImageStore.h"

#define kLeft 10
#define kTop 10
#define kTextFontsize 13

@interface UserStatusCell (private)
+ (CGFloat) getTextHeight:(Status*)status editing:(BOOL)editing;
+ (CGFloat) getCellWidth:(BOOL)editing;
@end

@implementation UserStatusCell

@synthesize status;
@synthesize editing;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        textLabel.font = [UIFont systemFontOfSize:kTextFontsize];
        textLabel.numberOfLines = 100;
        [self.contentView addSubview:textLabel];
        
        footerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        footerLabel.font = [UIFont systemFontOfSize:12];
        footerLabel.textColor = [UIColor colorWithRed:0.725f green:0.725f blue:0.725f alpha:1.0f];
        [self.contentView addSubview:footerLabel];
        
        imageButton = nil;
    }
    return self;
}

- (void) updateStatus:(Status*)astatus
{
    self.status = astatus;
    textLabel.text = status.text;
    footerLabel.text = [NSString stringWithFormat:@"%@ 来自%@", status.timestamp, [status sourceDescription]];
    
    if ( status.smallPicture && [status.smallPicture length] > 0 ) {
        if ( imageButton == nil ) {
            imageButton = [[UIButton alloc] initWithFrame:CGRectZero];
            [self.contentView addSubview:imageButton];
        }
        
        UIImage* image = [[TweetImageStore sharedInstance] getTweetImage:status.smallPicture square:80 delegate:self];
        //self.imageView.image = image;
        [imageButton setImage:image forState:UIControlStateNormal];
    }
    else {
        if ( imageButton ) {
            [imageButton removeFromSuperview];
            [imageButton release];
            imageButton = nil;
        }
        //self.imageView.image = nil;
    }
}


- (void) layoutSubviews 
{
    CGFloat y = kTop;
    CGFloat textHeight = [UserStatusCell getTextHeight:status editing:editing];
    CGFloat textWidth = [UserStatusCell getCellWidth:editing];
    textLabel.frame = CGRectMake(kLeft, y, textWidth, textHeight);
    y += textHeight + 10;;
    
    if ( status.smallPicture && [status.smallPicture length] > 0 ) {
        imageButton.frame = CGRectMake(kLeft, y, 80, 80);
        //self.imageView.frame = CGRectMake(kLeft, y, 80, 80); 
        y += 80 + 10;
    }    
    
    footerLabel.frame = CGRectMake(kLeft, y, textWidth, 20);
    [super layoutSubviews];
}


+ (CGFloat) getTextHeight:(Status*)status editing:(BOOL)editing
{
    CGFloat textWidth = [UserStatusCell getCellWidth:editing];
    UIFont* textFont = [UIFont systemFontOfSize:kTextFontsize];
    CGSize size = [status.text sizeWithFont:textFont constrainedToSize:CGSizeMake(textWidth, 900) lineBreakMode:UILineBreakModeCharacterWrap];
    return size.height;
}


+ (CGFloat) getCellHeight:(Status*)status editing:(BOOL)editing
{
    CGFloat height = kTop + [UserStatusCell getTextHeight:status editing:editing] + 30 + kTop;
    if ( status.smallPicture && [status.smallPicture length] > 0 ) height = height + 90;
    
    return height;
}

+ (CGFloat) getCellWidth:(BOOL)editing
{
    if ( editing ) {
        return 220;
    }
    else {
        return 300;
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void) imageDidGetNewImage:(UIImage*)image forURL:(NSString*)url
{
    [imageButton setImage:image forState:UIControlStateNormal];
    [imageButton setNeedsDisplay];
    //self.imageView.image = image;
    //[self.imageView setNeedsDisplay];
}


- (void)dealloc
{
    [status release];
    [textLabel release];
    [footerLabel release];
    [imageButton release];
    [super dealloc];
}

@end
